<template>
  <div>
    412342341324123
    <button @click="generateAddresses">Generate Address</button>
  </div>
</template>

<script setup>
const secp256k1 = await import('secp256k1');
const { keccak256 } = await import('ethereumjs-util/dist/index.js');
const { randomBytes } = await import('crypto');


function generateAddresses(count) {
  const addresses = [];
  for (let i = 0; i < count; i++) {
    try {
      // 生成随机私钥
      let privateKey;
      do {
        privateKey = randomBytes(32);
      } while (!secp256k1.privateKeyVerify(privateKey));

      // 生成公钥
      const publicKey = secp256k1.publicKeyCreate(privateKey, false).slice(1);

      // 确保公钥是 Buffer 类型
      const publicKeyBuffer = Buffer.from(publicKey);

      // 生成地址
      const address = keccak256(publicKeyBuffer).subarray(0,-20);
      addresses.push({
        privateKey: privateKey.toString('hex'),
        address: '0x' + address.toString('hex')
      });
    } catch (error) {
      console.error('Error generating address:', error);
    }
  }
  console.log(addresses)
  return addresses;
}
</script>
